package day_2022_12_to_3.day;

import java.util.Arrays;

/**
 * @author haomin
 * @date 2022/12/07 15:21
 **/
public class Day12_7_Num1775 {
    class Solution {
        public int minOperations(int[] nums1, int[] nums2) {
            if(6 * nums1.length < nums2.length || 6 * nums2.length < nums1.length)  return -1;
            int diff = Arrays.stream(nums2).sum() - Arrays.stream(nums1).sum();
            if(diff < 0) {
                diff *= -1;
                int[] tmp = nums1;
                nums1 = nums2;
                nums2 = tmp;
            }
            int[] cnt = new int[6];
            for(int num : nums1)    cnt[6 - num]++;
            for(int num : nums2)    cnt[num - 1]++;
            int ans = 0;
            for(int i = 5; i > 0 && diff > 0; --i) {
                int cur = Math.min(cnt[i], (diff + i - 1) / i);
                diff -= cur * i;
                ans += cur;
            }
            return ans;
        }
    }
}